#include "MainLoopState.h"
#include "FadeOutState.h"
#include "GameModuleState.h"
#include "VideoModuleState.h"
#include "PicModuleState.h"
#include "ResumeModuleState.h"
#include "Portfolio.h"
#include <allegro.h>
#include <winalleg.h>

using namespace std;


int MainLoopState::locked = 0;

MainLoopState::MainLoopState(void)
{
}

MainLoopState::~MainLoopState(void)
{
}

void MainLoopState::enter()
{
	quit = mouselocked = xm = ym = fadetime = fadeflag = 0;
	cout<<"Entered main loop";
	buttons = port->getRMgr()->buttons;
}

void MainLoopState::update(const float delta)
{
	if(mouse_b == 0)
		mouselocked = 0;

	//Check mouse
	for(int i = 0; i < ResourceManager::num_buttons; i++)
	{
		if(buttons[i].trans == 0)
			buttons[i].flag = 0;

		if(buttons[i].hide == 1 && buttons[i].drawpt > 0)
			buttons[i].drawpt-=1;

		if(buttons[i].trans == 1 && buttons[i].ypos > 130)
			buttons[i].ypos-=5;

		if(buttons[i].ypos == 130 && buttons[i].trans == 1)
			buttons[i].trans = 2;

		if(buttons[i].trans == 2 && mouse_b & 2)
		{
			for(int j = 0; j < ResourceManager::num_buttons; j++)
			{
				buttons[j].hide = 0;
				//Hack fix it later
				buttons[j].drawpt = 74;
			}

			buttons[i].trans = 0;
			buttons[i].flag = 0;
			buttons[i].ypos = buttons[i].oldy;
			locked = 0;
		}

		if(mouse_x > buttons[i].xpos && mouse_x < buttons[i].xpos + 60)
		{
			if(mouse_y > buttons[i].ypos && mouse_y < buttons[i].ypos + 60)
			{
				if(buttons[i].hide != 1 && buttons[i].trans != 1)
				{
					buttons[i].flag = 1;
					if(mouse_b == 1 && i == 5)
					{
						quit = 1;
					}
				}
			}
		}
		//SUBMENU
		if(buttons[i].trans == 2)
		{
			ym = -1;
			for(int j = 0; j < buttons[i].options; j++)
			{
				if((mouse_x > 130 && mouse_x < 400) && (mouse_y > 50*j + 239 && mouse_y < 50*j + 279))
				{
					ym = j;
					if(mouse_b == 1 && mouselocked == 0)
					{
						mouselocked = 1;
						//Access Sub Sections
						for(int k = 0; k < ResourceManager::num_buttons; k++)
							buttons[k].drawpt = 0 ;

						ym = -1;

						switch(i)
						{
						case 0: 
							switch(j)
							{
							case 0:
								port->changeState(new GameModuleState("3DEFFECTS"));
								return;
							case 1:
								port->changeState(new VideoModuleState("BLENDERDEMO"));
								return;
							case 2:
								port->changeState(new VideoModuleState("3DSTUDIO"));
								return;
							case 3: 
								port->changeState(new GameModuleState("DX9ENGINE"));
								return;
							}
							break;
						case 1:
							switch(j)
							{
							case 0:
								port->changeState(new GameModuleState("2DGAME"));
								return;
							case 1:
								port->changeState(new PicModuleState("2D"));
								return;
							case 2:
								port->changeState(new VideoModuleState("PSDEMO"));
								return;
							default:
								break;
							}
							break;
						case 2:
							switch(j)
							{
							case 0:
								port->changeState(new GameModuleState("SUPERSPRITEPACK"));
								return;
							default:
								break;
							}
						case 3:
							switch(j)
							{
							case 0:
								port->changeState(new ResumeModuleState("RESUME"));
								return;
							default:
								break;
							}
						case 4:
							switch(j)
							{
							case 0:
								port->changeState(new PicModuleState("WATASHI"));
								return;
							default:
								break;
							}
						default:
							break;
						}
					}//END if(mouse_b == 1)
				}
			}
		}

		if(locked == 0)
		{
			if(mouse_x > buttons[i].xpos && mouse_x < buttons[i].xpos + 60)
			{
				if(mouse_y > buttons[i].ypos && mouse_y < buttons[i].ypos + 60)
				{
					if(buttons[i].hide != 1 && buttons[i].trans != 1)
					{
						buttons[i].flag = 1;
						if(mouse_b == 1 && mouselocked == 0)
						{
							mouselocked = 1;
							//Deactivate all the other buttons
							for(int j = 0; j < ResourceManager::num_buttons; j++)
							{
								if(j != 5)
								{
									//hide all buttons exept exit
									buttons[j].hide = 1;
								}
							}
							//Except for button hit
							buttons[i].hide = 0;
							buttons[i].trans = 1;
							buttons[i].flag = 1;
							locked = 1;
						}
					}
				}
			}
		}//End if(locked == 0)
	}//End for(int i = 0; i < ResourceManager::num_buttons; i++)

	if(mouse_b == 1 && mouselocked == 0)
		mouselocked = 1;

	fadetime++;
	if(fadetime > 100)
	{
		fadetime = 0;
		fadeflag++;
	}
	if(fadeflag == 8)
		fadeflag = 0;


	if(quit)
	{
		port->changeState(new FadeOutState());
	}
}

void MainLoopState::render()
{
	//FIRST DRAW THE BACKDROP
	port->getGUtil()->drawToBackBuffer(
		(BITMAP *)port->getRMgr()->GraphicPackA[Mainmenu].dat,
		0, 0);

	//DRAW FOLDER ICONS
	for(int i = 0; i < ResourceManager::num_buttons; i++)
	{
		//Draw the main folders
		if(buttons[i].hide == 0)
		{
			if(i < MENUOPNUM)
			{
				port->getGUtil()->drawTextToBackBuffer(
					font,
					menutext[i],
					135,
					buttons[i].ypos + 35,
					C_MainFolderShadow);
				port->getGUtil()->drawTextToBackBuffer(
					font,
					menutext[i],
					130,
					buttons[i].ypos + 30,
					C_MainFolder);
			}
			if(buttons[i].flag == 0)
			{
				if(buttons[i].B_Normal != NULL)
				{
					port->getGUtil()->drawAlphaToBackBuffer(
						buttons[i].B_Normal,
						buttons[i].xpos,
						buttons[i].ypos);
				}
			}
			else
			{
				if(buttons[i].B_Rollover != NULL)
				{
					port->getGUtil()->drawAlphaToBackBuffer(
						buttons[i].B_Rollover,
						buttons[i].xpos,
						buttons[i].ypos);
				}
				if(i == 5)
				{
					port->getGUtil()->drawTextToBackBuffer(
						font,
						defaultmsg[8],
						702,
						532,
						C_MainFolderShadow);
					port->getGUtil()->drawTextToBackBuffer(
						font,
						defaultmsg[8],
						700,
						530,
						C_MainFolder);
				}
			}
		}
		else
		{
			if(buttons[i].B_Normal != NULL)
			{
				clear_bitmap(buttons[i].B_Scratch);

				blit(
					buttons[i].B_Normal,
					buttons[i].B_Scratch,
					0,0,0,0,
					buttons[i].B_Normal->w,
					buttons[i].drawpt);

				port->getGUtil()->drawAlphaToBackBuffer(
					buttons[i].B_Scratch,
					buttons[i].xpos,
					buttons[i].ypos);
			}
		}
	}

	const int BSHADOW_LEVEL = 1;
	const int BTEXT_LEVEL = 2;

	//DRAW TEXT AND LINES FOR SUBMENU 
	for(int i = 0; i < ResourceManager::num_buttons; i++)
	{
		if(buttons[i].trans == 2)
		{
			port->getGUtil()->clearBufferToColor(255, 0, 255, BSHADOW_LEVEL);
			port->getGUtil()->clearBufferToColor(255, 0, 255, BTEXT_LEVEL);

			do_line(
				port->getGUtil()->getLevel(BSHADOW_LEVEL),
				70, 
				204, 
				70, 
				204 + (buttons[i].options * 50), 
				4, 
				circleline);

			//Highlight Box
			if(ym != -1)
			{	
				rect	(port->getGUtil()->getLevel(BSHADOW_LEVEL), 127, 50*ym + 237, 554, 50*ym + 267, makecol(255,255,255));
				rectfill(port->getGUtil()->getLevel(BSHADOW_LEVEL), 130, 50*ym + 239, 550, 50*ym + 264, makecol(255,255,255));
			}	

			for(int j = 0; j < buttons[i].options; j++ )
			{
				do_line(port->getGUtil()->getLevel(BSHADOW_LEVEL),70, 50*j + 254, 120, 50*j + 254, 4, circleline);

				port->getGUtil()->drawTextToBackBuffer(
					font, 
					buttons[i].text_option[j],
					132,50*j + 243,
					makecol(0,0,0),
					-1,
					BSHADOW_LEVEL);
			}	

			//Render Final
			port->getGUtil()->drawTransToBackBuffer(port->getGUtil()->getLevel(BSHADOW_LEVEL), 10, 10, 50);
			port->getGUtil()->drawToBackBuffer(port->getGUtil()->getLevel(BSHADOW_LEVEL),0,0);

			port->getGUtil()->drawTextToBackBuffer(font, defaultmsg[4], 52, 502, C_MainFolderShadow	);
			port->getGUtil()->drawTextToBackBuffer(font, defaultmsg[4], 50, 500, C_MainFolder		);
			port->getGUtil()->drawTextToBackBuffer(font, defaultmsg[3], 52, 532, C_MainFolderShadow	);
			port->getGUtil()->drawTextToBackBuffer(font, defaultmsg[3], 50, 530, C_MainFolder		);
		}
	}

	if(locked == 0)
	{
		port->getGUtil()->drawTextToBackBuffer(font, defaultmsg[2] ,52,532, C_MainFolderShadow);
		port->getGUtil()->drawTextToBackBuffer(font, defaultmsg[2], 50,530, C_MainFolder);
	}

	rectfill(port->getGUtil()->getLevel(0),mouse_x - 3,mouse_y - 3,mouse_x + 3, mouse_y + 3, makecol(0,0,0));
	rectfill(port->getGUtil()->getLevel(0),mouse_x - 2,mouse_y - 2,mouse_x + 2, mouse_y + 2, makecol(255,255,255));

	// If we are quiting save the back buffer for the fade out
	if (!quit)
	{
		port->getGUtil()->drawBackBufferToScreen();
		port->getGUtil()->clearBuffers();
	}
}

void MainLoopState::leave()
{
	//for (int i = 0; i < ResourceManager::num_buttons; ++i)
	//{
	//	port->getRMgr()->buttons[i].flag = 0;
	//	port->getRMgr()->buttons[i].trans = 0;
	//	port->getRMgr()->buttons[i].hide = 0;
	//}
}